<!DOCTYPE html>
<?php
require_once'functions.php';
require_once'config.php';
$studentID = $_SESSION['studentID'];
$checkAnswer = checkAnswer($currentQuestion);
$getAttempts = getAttempts($studentID);
$attempt = $_POST['attempt'];
$_SESSION['attempt'] = $_POST['attempt'];
$attempt = ($attempt);
?>
<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.11.0.min.js"></script>
<script src="scripts/scripts.js" type="text/javascript"></script>
<link rel="stylesheet" href="styles/styles.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() 
{
 	var form = $('#getCompare'); // contact form
	var alert = $('.rightFeedback'); // alert div for show alert message
  // form submit event
  form.change(function(e)  {
  e.preventDefault(); // prevent default form submit
    $.ajax({
      url: 'comparison.php', // form action url
      type: 'POST', // form submit method get/post
      dataType: 'html', // request type html/json/xml
      data: form.serialize(), // serialize form data 
      beforeSend: function() {
      alert.fadeOut();
      },
      success: function(data) {
      alert.html(data).fadeIn(); // fade in response data
      },
      error: function(e) {
        console.log(e)
      }
    });
  });
});
</script>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Formative Assessment</title>
</head>
<body>
<header>Learner Centred Design Formative Assessment<span class="loggedIn">Logged in as:<?php echo ucfirst($_SESSION['firstName']);?>&nbsp;&nbsp;<a href="logout.php">Logout</a></span></header>
<!--<nav>
  <?php include 'nav.php' ?>
</nav>-->
<div id="container" class="one-edge-shadow">
<div id="mainContent">
	<form method='post' action="postquiz.php" id="retakeForm">
    <input type='submit' id='retake' name='retake' value='Fancy another go?'  class='btn'>
	<input type="hidden" name="num" value="<?php echo $num ?>">
    <input type="hidden" name="score" value="<?php echo $score ?>">
    <input type="hidden" name="retaker" value="TRUE">
    <input type="hidden" name="attempt" value="<?php echo $_SESSION['attempt'] ?>">
    </form> 
 
<div id="attemptSelector">Choose another of your attempts to compare your results:<form method="post" action="comparison.php" id="setAttempt">
<select name="getCompare" id="getCompare">
<?php
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT DISTINCT attempt, MAX(startTime) FROM studentintest WHERE studentID='$studentID' AND testID='1' GROUP BY attempt ORDER by startTime ASC";
	$result = mysqli_query($con,$query) or die ("Could not execute query"); 
	while ($row = mysqli_fetch_array($result)) 
{
	$dAttempt = $row[0];
	$dStartTime = $row[1]; 
echo "<option value=" . $dAttempt . ">Attempt number " . $dAttempt  .   " taken on " . $dStartTime . "</option>";
}	
 ?> 
</select>
</form>
</div>
<div class="feedbackContainer">
<div class='rightFeedback hidden'></div>
<div class="leftFeedback"> 
<h1 class="resultTitle">Your first attempt</h1>
<?php 
//////////////////////////////////////////////////////////////////ATTEMPT 1 - LEFT
$testID = 1;
$studentID = $_SESSION['studentID'];
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
$query = "SELECT * FROM studentresponse WHERE studentID ='$studentID' AND testID='$testID' AND attempt='1' ORDER BY startTime ASC";
$result = mysqli_query($con,$query) or die ("Could not execute query");     
$nrows = mysqli_num_rows($result);
for ($i = 0; $i < $nrows; $i++ )
{	
	$row = mysqli_fetch_assoc($result);
	extract($row); 
{ 
$arrayAnswer = explode(",", $selectedAnswer);
sort($arrayAnswer);
$sorted = (implode(",", $arrayAnswer));
	if ($sorted == $correctAnswer)
				{ 
					$feedbackIcon = "<img class='feedbackIcon' src='img/checkmark.png' alt='incorrect icon'/>";
					//echo "<img class='feedbackIcon' src='img/checkmark.png' />";
				} 
					else 
				{ 
					$feedbackIcon = "<img class='feedbackIcon' src='img/delete.png' alt='correct icon'/>";

				//	echo "<img class='feedbackIcon' src='img/delete.png'/>";
				} 
 if (strlen($sorted) == strlen($correctAnswer))
{
	if (strpos($sorted, $correctAnswer) !== false)
 {
$arrayFeedbackStatus = "<span class='green'> Correct.</span>";
$feedbackIcon = "<img class='feedbackIcon' src='img/checkmark.png' alt='correct icon' />";
}
 }
else
{
$arrayFeedbackStatus = "<span class='red'> Incorrect.</span>";
$feedbackIcon = "<img class='feedbackIcon' src='img/delete.png' alt='incorrect icon' />";
 }
?>
<div class="fb">
<?php echo $feedbackIcon?>
<h2 class="reportItem">Question <?php echo $questionID?></h2>
<h2 class="reportItem"><?php ?></h2>
<p class="reportItem"><span class="emphasis">Selected Answer:</span> <?php echo $sorted; ?></p>
<p class="reportItem"><span class="emphasis">Your answer was:</span> <?php echo $arrayFeedbackStatus ?></p>
<p class="reportItem"><span class="emphasis">Recommended resources:</span>Recommended resources for each question would be displayed here. Correct answers and incorrect answers would receive different recommonedations so that those who did not get the answer correct are not given resources that are based on knowledge they have been unable to demonstrate.</p>  
</div> 
  <?php
  }
  }
$finalScore = finalScore($studentID, 1) ?>
<h2>Final score:<?php echo $finalScore ?>/10</h2> 
<?php $avgFinalScore = avgFinalScore($studentID, 1) ?>
<p class="avg">The average score for the first attempt among your peers is <?php echo $avgFinalScore ?>.</p> 
</div>
</div>


<!-- <p class='smiling'>attempt:<?php echo $_SESSION['attempt'];?> 
   <p class='smiling'>firstName:<?php echo $_SESSION['firstName']?> </p>
    <p class='smiling'>score:<?php echo $_SESSION['score'];?> </p>
    </p>
    <p class='smiling'>username: <?php echo $_SESSION['username'];?> </p>
    <p class='smiling'>id: <?php echo $_SESSION['studentID'];?> </p>-->


</div>
</div> 
</body>
</html>